<template>
    <component
      is="a-modal"
      v-model:visible="visible"
      :width="600"
      :title="title"
      :mask-closable="false"
      :ok-loading="loading"
      @cancel="close"
      @before-ok="submit">
      <!-- 表单信息 start -->
      <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
        <a-row :gutter="10">
          <a-col :span="12">
            <a-form-item label="用户账号" field="account">
              {{ formData.account }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户昵称" field="nickname">
              {{ formData.nickname }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="真实姓名" field="real_name">
              {{ formData.real_name }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户电话" field="mobile">
              {{ formData.mobile }}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="当前余额" field="user_money">
              <span class="text-red-500">￥{{ formData.user_money }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="累计充值" field="total_recharge_amount">
              <span class="text-red-500">￥{{ formData.total_recharge_amount }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="充值金额" field="recharge_money">
              <a-input-number v-model="formData.recharge_money" :min="0.01" :max="10000" :precision="2" :step="1" placeholder="请输入充值金额" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="充值备注" field="recharge_extra">
              <a-textarea v-model="formData.recharge_extra" placeholder="请输入充值备注" allow-clear />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表单信息 end -->
    </component>
  </template>
  
  <script setup>
  import { ref, reactive, computed } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import api from '../../api/user/user'
  
  const emit = defineEmits(['success'])
  // 引用定义
  const visible = ref(false)
  const loading = ref(false)
  const formRef = ref()
  const mode = ref('')
  
  let title = computed(() => {
    return '用户-充值'
  })
  
  // 表单信息
  const formData = reactive({
    id: null,
    avatar: '',
    nickname: '',
    real_name: '',
    sex: null,
    mobile: '',
    email: '',
    channel: 1,
    status: 1,
    is_new_user: 1,
    login_ip: '',
    login_time: '',
    user_money: '0.00',
    total_recharge_amount: '0.00',
    user_address: '',
    latitude: '',
    longitude: '',
  })
  
  // 验证规则
  const rules = {
    recharge_money: [{ required: true, message: '充值金额必需填写' }],
    recharge_extra: [{ required: true, message: '充值备注必需填写' }],
  }
  
  // 打开弹框
  const open = async (type = 'add') => {
    mode.value = type
    visible.value = true
    formRef.value.resetFields()
    await initPage()
  }
  
  // 初始化页面数据
  const initPage = async () => {
  }
  
  // 设置数据
  const setFormData = async (data) => {
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        formData[key] = data[key]
      }
    }
  }
  
  // 数据保存
  const submit = async (done) => {
    const validate = await formRef.value?.validate()
    if (!validate) {
      loading.value = true
      let data = { recharge_money: formData.recharge_money, recharge_extra: formData.recharge_extra }
      let result = await api.recharge(formData.id, data)
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    }
    done(false)
  }
  
  // 关闭弹窗
  const close = () => (visible.value = false)
  
  defineExpose({ open, setFormData })
  </script>
  